﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using WmsService.BusinessLogic;
using WmsService.Factory;
using WmsService.In;
using WmsService.Out;
using WmsService.Util;
using WmsService.Entity;

namespace WmsService
{
    public partial class Service
    {
        #region 业务操作

        //入库部分
        public InStoreBillWrap GetInStoreBillWrap(string billID)
        {
            if (billID == null) throw new ArgumentNullException("billID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            return logic.GetInStoreBillWrap(billID);
        }

        public void SaveInStoreBillWrap(InStoreBillWrap inStoreBillWrap)
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            logic.SaveInStoreBillWrap(inStoreBillWrap);
        }

        public void AffirmInStoreBill(string billID)
        {
            if (billID == null) throw new ArgumentNullException("billID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            logic.AffirmInStoreBill(billID, DateTime.Now.Date);
        }

        public void AffirmInStoreBill(string billID, DateTime? inBillDate)
        {
            if (billID == null) throw new ArgumentNullException("billID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            logic.AffirmInStoreBill(billID, inBillDate);
        }

        public void AffirmInStoreBillInvoice(string billID, DateTime? inStoreBillInvoiceDate)
        {
            if (billID == null) throw new ArgumentNullException("billID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            logic.AffirmInStoreBillInvoice(billID, inStoreBillInvoiceDate);
        }

        public string GetInStoreBillID()
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            return logic.GetInStoreBillID();
        }

        public string ImportInStoreBillFromOutStoreBill(string outStorebillID)
        {
            if (outStorebillID == null) throw new ArgumentNullException("outStorebillID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            return logic.ImportInStoreBillFromOutStoreBill(outStorebillID);
        }

        public InStoreBillWrap ImportInStoreBillFromOutStoreBill2(string outStorebillID)
        {
            if (outStorebillID == null) throw new ArgumentNullException("outStorebillID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            return logic.ImportInStoreBillFromOutStoreBill2(outStorebillID);
        }

        public List<OutStoreBill> GetAbnormalOutStoreBill(string condition)
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            return logic.GetAbnormalOutStoreBill(condition);
        }

        public List<String> GetAbnormalInStoreBillCodes()
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInStoreBillLogic();
            return logic.GetAbnormalInStoreBillCodes();
        }

        public List<OutInStoreBillType> GetOutInStoreBillTypes()
        {
            return OutInStoreBillTypeFactory.GetOutInStoreBillTypes();
        }

        // 出库部分

        public OutStoreBillWrap GetOutStoreBillWrap(string billID)
        {
            if (billID == null) throw new ArgumentNullException("billID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateOutStoreBillLogic();
            return logic.GetOutStoreBillWrap(billID);
        }

        public void SaveOutStoreBillWrap(OutStoreBillWrap outStoreBillWrap)
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateOutStoreBillLogic();
            logic.SaveOutStoreBillWrap(outStoreBillWrap);
        }

        public void AffirmOutStoreBill(string billID)
        {
            if (billID == null) throw new ArgumentNullException("billID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateOutStoreBillLogic();
            logic.AffirmOutStoreBill(billID, DateTime.Now.Date);
        }

        public void AffirmOutStoreBill(string billID, DateTime? outBillDate)
        {
            if (billID == null) throw new ArgumentNullException("billID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateOutStoreBillLogic();
            logic.AffirmOutStoreBill(billID, outBillDate);
        }

        public string GetOutStoreBillID()
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateOutStoreBillLogic();
            return logic.GetOutStoreBillID();
        }

        public List<String> GetOutStoreBillDepts()
        {
            var ctx = ContextFactory.Create();
            var q = (from c in ctx.OutStoreBill
                     where !String.IsNullOrEmpty(c.Department)
                     select c.Department).Distinct();
            return q.ToList();
        }

        //付款单中的付款项

        public string NewPayApplyBillItem(PayApplyBillItem payApplyBillItem)
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInvoiceAndPayApplyBillLogic();
            return logic.NewPayApplyBillItem(payApplyBillItem);
        }

        public PayApplyBillItem GetPayApplyBillItem(string payApplyBillItemID)
        {
            if (payApplyBillItemID == null) throw new ArgumentNullException("payApplyBillItemID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInvoiceAndPayApplyBillLogic();
            return logic.GetPayApplyBillItem(payApplyBillItemID);
        }

        public List<PayApplyBillItem> GetPayApplyBillItemsByCondition(string condition)
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInvoiceAndPayApplyBillLogic();
            return logic.GetPayApplyBillItemsByCondition(condition);
        }

        public void CancelPayApplyBillItem(string payApplyBillItemID)
        {
            if (payApplyBillItemID == null) throw new ArgumentNullException("payApplyBillItemID");
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInvoiceAndPayApplyBillLogic();
            logic.CancelPayApplyBillItem(payApplyBillItemID);
        }

        public List<String> GetPayApplyBillDepts()
        {
            var ctx = ContextFactory.Create();
            var q = (from c in ctx.PayApplyBill
                     where !String.IsNullOrEmpty(c.PayApplyDept)
                     select c.PayApplyDept).Distinct();
            return q.ToList();
        }

        #endregion

        #region Extra

        public List<DistinctEntity> GetDistinctEntities(string table, string field)
        {
            const string sql = "select distinct {0} as Field from dbo.{1}";
            var ctx = ContextFactory.Create();
            return ctx.ExecuteStoreQuery<DistinctEntity>(String.Format(sql, field, table)).ToList();
        }

        #endregion

        #region 2010-10-28

        //================2010-10-28 added by henrik
        public void AddInvoiceBillInStoreBillDtl(List<InvoiceBillInStoreBillDtl> invoiceBillInStoreBillDtls)
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInvoiceAndPayApplyBillLogic();
            logic.AddInvoiceBillInStoreBillDtl(invoiceBillInStoreBillDtls);
        }

        public List<InvoiceBillInStoreBillDtl> GetInvoiceBillInStoreBillDtls(string condition)
        {
            var ctx = ContextFactory.Create();
            return
                ctx.ExecuteStoreQuery<InvoiceBillInStoreBillDtl>(
                    "select * from  dbo.InvoiceBillInStoreBillDtl where  1=1 " + condition).ToList();
        }

        public void CancelInStoreBillDtlFromInvoiceBill(string invoiceNo, string inStoreBillDtlID)
        {
            var logic = LogicFactoryCreator.CreateAbstractFactory().CreateInvoiceAndPayApplyBillLogic();
            logic.CancelInStoreBillDtlFromInvoiceBill(invoiceNo, inStoreBillDtlID);
        }

        public string GetLowestVersion()
        {
            var value = ConfigurationManager.AppSettings["LowestVersion"];
            if (String.IsNullOrEmpty(value))
                return "1";
            return value;
        }

        //==================================

        #endregion

        #region 2011-01-05
        public List<VInStoreBillDtlAndHead> GetVInStoreBillDtlAndHeadsByCondition(string condition)
        {
            var ctx = ContextFactory.Create();
            return
                ctx.ExecuteStoreQuery<VInStoreBillDtlAndHead>(
                    "select * from  dbo.VInStoreBillDtlAndHead where  1=1 " + condition).ToList();
        }
        #endregion
    }
}